import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const routers = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    name: 'home',
    hidden: true
  },

  {
    path: '/external',
    component: Layout,
    name: 'ui-norm.external',
    redirect: '/external/bookexternal/addexternal',
    meta: {
      title: '法定呈缴',
      icon: 'nested'
    },
    hidden: true,
    children: [
      {
        path: 'bookexternal',
        name: 'ui-norm.bookexternal',
        redirect: '/external/bookexternal/addexternal',
        component: () => import('@/views/demo/AddExternal'),
        meta: { title: '图书呈缴' },
        hidden: true,
        children: [
          {
            path: 'addexternal',
            component: () => import('@/views/demo/AddExternal'),
            name: 'ui-norm.addexternal',
            meta: { title: '新增缴送' },
            hidden: true
          }
        ]
      }
    ]
  },

  {
    path: '/internal',
    component: Layout,
    name: 'ui-norm.internal',
    meta: {
      title: '征集',
      icon: 'form'
    },
    redirect: '/internal/packageconfirm',
    hidden: true,
    children: [
      {
        path: 'packageconfirm',
        name: 'ui-norm.packageconfirm',
        meta: { title: '包裹确认' },
        hidden: true,
        component: () => import('@/views/demo/PackageConfirm')
      }
    ]
  },

  {
    path: '/preservation',
    component: Layout,
    name: 'ui-norm.preservation',
    meta: {
      title: '保藏',
      icon: 'el-icon-s-help'
    },
    redirect: '/preservation/libraryreception',
    hidden: true,
    children: [
      {
        path: 'libraryreception',
        name: 'ui-norm.libraryreception',
        meta: { title: '藏馆接收' },
        hidden: true,
        component: () => import('@/views/demo/LibraryReception')
      }
    ]
  },

  {
    path: '/settings',
    component: Layout,
    name: 'settings.norm.enabled',
    meta: {
      title: '系统设置',
      icon: 'el-icon-s-check'
    },
    redirect: '/settings/dictionary',
    hidden: true,
    children: [
      {
        path: 'dictionary',
        name: 'settings.norm.enabled',
        meta: { title: '字典表设置' },
        hidden: true,
        component: () => import('@/views/settings/dictionary')
      }
    ]
  }
]

const routesData = routers

if (window.__POWERED_BY_QIANKUN__) {
  routesData.map((item) => {
    if (item.path.includes('/')) {
      item.path = '/subvue' + item.path
    }
    if (item.redirect) {
      item.redirect = '/subvue' + item.redirect
    }
    return item
  })
}

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: routesData
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
